package com.jqkb.pages;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

import com.jqkb.R;
import com.jqkb.TheApplication;
import com.jqkb.utils.Constant;

public class ChangeServerActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {


    private EditText changeIpEdt;
    private RadioGroup group;
    private CheckBox showAliveV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_server);
        changeIpEdt = (EditText) findViewById(R.id.ipEdt);
        group = (RadioGroup) findViewById(R.id.group);
        group.setOnCheckedChangeListener(this);
        changeIpEdt.setText(TheApplication.getServer());
        showAliveV = ((CheckBox) findViewById(R.id.showAliveV));

        boolean showAlive = TheApplication.getSharedPreferences().getBoolean("showAlive", false);
        showAliveV.setChecked(showAlive);
    }

    public void changeIp(View view) {

        String ipStr = changeIpEdt.getText().toString().trim();
        if (!("" == ipStr)) {

            SharedPreferences sharedPreferences = TheApplication.getSharedPreferences();
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(Constant.SERVER, ipStr);
            editor.putBoolean(Constant.SHOW_ALIVE, showAliveV.isChecked());
            editor.commit();
            Toast.makeText(ChangeServerActivity.this, "已更改", Toast.LENGTH_SHORT).show();
            view.postDelayed(new Runnable() {
                @Override
                public void run() {
                    finish();
                    Intent intent = new Intent(ChangeServerActivity.this, LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }, 500);

        } else {
            Toast.makeText(ChangeServerActivity.this, "不能为空", Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
        changeIpEdt.setText(radioButton.getText());

    }
}
